package org.dromara.ai.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ai.domain.vo.AiWorkVo;
import org.dromara.ai.domain.bo.AiWorkBo;
import org.dromara.ai.service.IAiWorkService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 作品
 *
 * @author Tang
 * @date 2024-12-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/work")
public class AiWorkController extends BaseController {

    private final IAiWorkService aiWorkService;

    /**
     * 查询作品列表
     */
    @SaCheckPermission("ai:work:list")
    @GetMapping("/list")
    public TableDataInfo<AiWorkVo> list(AiWorkBo bo, PageQuery pageQuery) {
        return aiWorkService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出作品列表
     */
    @SaCheckPermission("ai:work:export")
    @Log(title = "作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AiWorkBo bo, HttpServletResponse response) {
        List<AiWorkVo> list = aiWorkService.queryList(bo);
        ExcelUtil.exportExcel(list, "作品", AiWorkVo.class, response);
    }

    /**
     * 获取作品详细信息
     *
     * @param workId 主键
     */
    @SaCheckPermission("ai:work:query")
    @GetMapping("/{workId}")
    public R<AiWorkVo> getInfo(@NotNull(message = "主键不能为空")
                               @PathVariable Long workId) {
        return R.ok(aiWorkService.queryById(workId));
    }

    /**
     * 新增作品
     */
    @SaCheckPermission("ai:work:add")
    @Log(title = "作品", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AiWorkBo bo) {
        return toAjax(aiWorkService.insertByBo(bo));
    }

    /**
     * 修改作品
     */
    @SaCheckPermission("ai:work:edit")
    @Log(title = "作品", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AiWorkBo bo) {
        return toAjax(aiWorkService.updateByBo(bo));
    }

    /**
     * 删除作品
     *
     * @param workIds 主键串
     */
    @SaCheckPermission("ai:work:remove")
    @Log(title = "作品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] workIds) {
        return toAjax(aiWorkService.deleteWithValidByIds(List.of(workIds), true));
    }
}
